package com.budwk.app.enterprise.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.app.enterprise.models.EnterpriseSecurity;
import com.budwk.app.enterprise.services.EnterprisesecurityService;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@IocBean
@At("/platform/enterprise/security")
@ApiDefinition(tag = "企业安全专项")
@Slf4j
public class EnterpriseSecurityController {
    @Inject
    private EnterprisesecurityService wxEnterpriseSecurityService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询企业安全专项")
    @ApiFormParams(implementation = EnterpriseSecurity.class)
    @ApiResponses(implementation = EnterpriseSecurity.class)
    @SaCheckPermission("enterprise.security.listByPage")
    public Result<?> list(@Param("..")EnterpriseSecurity bean) {
        return wxEnterpriseSecurityService.listByPage(bean);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增企业安全专项")
    @ApiFormParams(
        implementation = EnterpriseSecurity.class
    )
    @ApiResponses
    @SLog(tag = "新增企业安全专项", value = "${args[0].id}")
    @SaCheckPermission("enterprise.security.create")
    public Result<?> create(@Param("..")EnterpriseSecurity dfxyEnterprise, HttpServletRequest req) {
        dfxyEnterprise.setCreatedBy(SecurityUtil.getUserId());
        wxEnterpriseSecurityService.insert(dfxyEnterprise);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改企业安全专项")
    @ApiFormParams(
        implementation = EnterpriseSecurity.class
    )
    @ApiResponses
    @SLog(tag = "修改企业安全专项", value = "${args[0].id}")
    @SaCheckPermission("enterprise.security.update")
    public Result<?> update(@Param("..")EnterpriseSecurity dfxyEnterprise, HttpServletRequest req) {
        dfxyEnterprise.setUpdatedBy(SecurityUtil.getUserId())
                .setCreatedAt(LocalDateTime.now()).setUpdatedAt(LocalDateTime.now()).setUpdatedBy(SecurityUtil.getUserId());
        wxEnterpriseSecurityService.updateIgnoreNull(dfxyEnterprise);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取企业安全专项")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("enterprise.security.getData")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        EnterpriseSecurity dfxyEnterprise = wxEnterpriseSecurityService.fetch(id);
        if (dfxyEnterprise == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(dfxyEnterprise);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除企业安全专项")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除企业安全专项", value = "${args[0]}")
    @SaCheckPermission("enterprise.security.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        EnterpriseSecurity dfxyEnterprise = wxEnterpriseSecurityService.fetch(id);
        if (dfxyEnterprise == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxEnterpriseSecurityService.delete(id);
        req.setAttribute("_slog_msg", dfxyEnterprise.getId());
        return Result.success();
    }
}
